import OpenAI from "openai";
import fs from 'fs';

//在运行环境中读取github 的 token
const token = "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"
const endpoint = "https://models.inference.ai.azure.com"
const modelName = "gpt-4o"
function getImageDataUrl(imgUrl, imgFormat) {
    const imageBuffer = fs.readFileSync(imgUrl);
    const imageBase64 = imageBuffer.toString('base64')
    return `data:image/${imgFormat};base64,${imageBase64}`;
  }
  async function main() {
    const client = new OpenAI({baseURL: endpoint, apiKey: token});

    const response = await client.chat.completions.create({
       model: modelName,
       temperature:1.0,
       max_tokens:1000,
       top_p:1.0,
       messages: [
        {role: 'system', content: '你是一个详细描述图片的好帮手。'},
        {role: 'user', content: [
          {type: 'text', text: '这张图片中有什么？'},
          {type: 'image_url', image_url: {
            url: getImageDataUrl("./test.jpg", "jpg"),
            detail: 'low'
          }}
        ]},
      ]
    });
    console.log(response.choices[0].message.content);
    

}
main()